package com.gaohe.web.controller.scp;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.gaohe.scp.domain.CsCar;
import com.github.pagehelper.PageInfo;

import com.gaohe.common.utils.SecurityUtils;
import com.gaohe.scp.domain.vo.CarAutoDto;
import com.gaohe.scp.domain.vo.CsRecordVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.scp.domain.CsAuction;
import com.gaohe.scp.service.ICsAuctionService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

/**
 * 竞拍记录Controller
 *
 * @author liuxiangyu
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/scp/auction")
public class CsAuctionController extends BaseController
{
    @Autowired
    private ICsAuctionService csAuctionService;


    //    根据carId查询竞拍记录---牛相善
//    @PreAuthorize("@ss.hasPermi('scp:auction:query')")
    @GetMapping(value = "/car/{carId}")
    public TableDataInfo getByCarId(@PathVariable("carId") Long carId) {
        List<CsAuction> list = csAuctionService.selectCsAuctionByCarId(carId);
//        System.out.println(csAuctionService.selectCsAuctionByCarId(carId));
        return getDataTable(list); // 返回分页数据
    }
    /**
     * 查询竞拍记录列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsAuction csAuction)
    {
        startPage();
        List<CsAuction> list = csAuctionService.selectCsAuctionList(csAuction);
        return getDataTable(list);
    }

    /**
     * 查询竞拍记录列表 zhangwenhao
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:list')")
    @GetMapping("/list/gld")
    public AjaxResult listGld(@RequestParam Map<String, String> map)
    {
        int page = Integer.parseInt(map.get("pageNum"))-1;
        int size = Integer.parseInt(map.get("pageSize"));
        CsAuction csAuction = new CsAuction();
        List<CsAuction> list = csAuctionService.selectCsAuctionListGld(csAuction);
        System.err.println(list);
        if (list == null|| list.size() == 0 ){
            return success(null);
        }
        Page<CsAuction> csAuctions = paginateList(list, page, size);
        return success(csAuctions);
    }


    /**
     * 导出竞拍记录列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:export')")
    @Log(title = "竞拍记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsAuction csAuction)
    {
        List<CsAuction> list = csAuctionService.selectCsAuctionList(csAuction);
        ExcelUtil<CsAuction> util = new ExcelUtil<CsAuction>(CsAuction.class);
        util.exportExcel(response, list, "竞拍记录数据");
    }

    /**
     * 获取竞拍记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:query')")
    @GetMapping(value = "/{auctionId}")
    public AjaxResult getInfo(@PathVariable("auctionId") Long auctionId)
    {
        return success(csAuctionService.selectCsAuctionByAuctionId(auctionId));
    }


    /**
     * 新增竞拍记录
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:add')")
    @Log(title = "竞拍记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsAuction csAuction)
    {
        return toAjax(csAuctionService.insertCsAuction(csAuction));
    }

    /**
     * 手动出价 新增竞拍记录 /修改
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:add')")
    @Log(title = "竞拍记录", businessType = BusinessType.INSERT)
    @PostMapping("/addAuctionOrUpdate")
    public AjaxResult addAuctionOrUpdate(@RequestBody CsAuction csAuction)
    {
        return toAjax(csAuctionService.insertCsAuctionOrUpdate(csAuction));
    }

    /**
     * 自动出价 新增竞拍记录 /修改
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:add')")
    @Log(title = "竞拍记录", businessType = BusinessType.INSERT)
    @PostMapping("/addAuctionOrUpdateAuto")
    public AjaxResult addAutoAuctionOrUpdateAuto(@RequestBody CarAutoDto carAutoDto)
    {
        int i = csAuctionService.insertCsAuctionOrUpdateAuto(carAutoDto);
        return AjaxResult.success(i);
    }

    /**
     * 修改竞拍记录
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:edit')")
    @Log(title = "竞拍记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsAuction csAuction)
    {
        return toAjax(csAuctionService.updateCsAuction(csAuction));
    }

    /**
     * 删除竞拍记录
     */
//    @PreAuthorize("@ss.hasPermi('scp:auction:remove')")
    @Log(title = "竞拍记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{auctionIds}")
    public AjaxResult remove(@PathVariable Long[] auctionIds)
    {
        return toAjax(csAuctionService.deleteCsAuctionByAuctionIds(auctionIds));
    }

    //根据车辆id查询车辆名称
//    @PreAuthorize("@ss.hasPermi('scp:auction:list')")
    @GetMapping("/getCarName/{carId}")
    public AjaxResult getCarName(@PathVariable Long carId)
    {
        return success(csAuctionService.selectCarNameByCarId(carId));
    }

    //分页工具
    public <T> Page<T> paginateList(List<T> list, int page, int size) {
        int totalItems = list.size();
        int totalPages = (int) Math.ceil((double) totalItems / size);

        List<T> pageContent = list.stream()
                .skip(page * size)
                .limit(size)
                .collect(Collectors.toList());

        return new PageImpl<>(pageContent, PageRequest.of(page, size), totalItems);
    }


    @GetMapping("/record")
    public TableDataInfo getRecordByUserId(){
        System.out.println(666);
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<CsRecordVo> o = csAuctionService.selectCsAuctionByUserId(userId);
        System.out.println(o);

        return getDataTable(o);
    }

//    @PreAuthorize("@ss.hasPermi('scp:auction:list')")
    @GetMapping("/myAuction/{carId}")
    public AjaxResult getMyAuctionByCarId(@PathVariable("carId") Long carId) {
        BigDecimal list = csAuctionService.getMyAuctionByCarId(carId);
        return AjaxResult.success(list);
    }


}
